﻿/*-------------------------------------------------------------------------
* 命名空间: XrayDirection.ViewModels/EditRecipeViewModel
* 类       名: EditRecipeViewModel
* 功      能:  TD
* 时      间:  2024-04-02 15:20:09
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using XrayDirection.Common;
using XrayDirection.DAL.DataEntity;
using XrayDirection.DAL.LocalDataAccess;
using XrayDirection.Services;
using XrayDirection.Views;

namespace XrayDirection.ViewModels
{
    public class EditRecipeViewModel : BindableBase
    {
        private IContainerProvider _containerProvider;
        private IWindowService _windowService;
        private IEventAggregator _eventAggregator;
        public EditRecipeViewModel(IContainerProvider containerProvider, IWindowService windowService, IEventAggregator eventAggregator)
        {
            _containerProvider = containerProvider;
            _windowService = windowService;
            _eventAggregator = eventAggregator;
            SaveOrderCommand = new DelegateCommand(DoSaveOrderTask);
            CancelOrderCommand = new DelegateCommand(DoCancelOrderComman);
        }

        private void DoCancelOrderComman()
        {
            _windowService.CloseWindow(nameof(EditRecipeView));
        }

        private void DoSaveOrderTask()
        {
            // 保存工单
            DataResult result = OrderAccess.UpdateOrder(OrderEdit!);
            if (result.State)
            {
                _eventAggregator.GetEvent<RecipeChangeEvent>().Publish(string.Empty);
                _containerProvider.Resolve<IndexViewModel>().RefreshWorkingInfo();
                //MessageProvider.ShowInfo("保存成功.", "工单保存提示框");
                _windowService.CloseWindow(nameof(EditRecipeView));
            }
            else
            {
                MessageProvider.ShowError($"保存失败: {result.Message}", "工单保存失败提示框");
            }
        }

        internal void RefreshEditOrder()
        {
            OrderEdit = _containerProvider.Resolve<IndexViewModel>().WorkingOrders[0];
        }



        public DelegateCommand SaveOrderCommand { get; private set; }
        public DelegateCommand CancelOrderCommand { get; private set; }


        private OrderEntity? orderEdit;

        public OrderEntity? OrderEdit
        {
            get { return orderEdit; }
            set
            {
                orderEdit = value;
                RaisePropertyChanged();
            }
        }

    }
}
